Okay.
So today we are going to learn about variables.
Now variables are a really really important part of scripting as they allow you to shorten down and
make your code much easier to read.
Okay.
Um, so if I add a script into, we're going to add it inside of this part here.
Okay.
Um, one of the thing you might have noticed from previous lessons when we were looking at properties
is that it gets very repetitive.
Um, whenever you want to reference a part, you keep having to say game dot workspace dot part every
single time.
Okay.
And that's relatively short for um, code to reference.
If you had a couple of folders saying like um, map.
So map to guess then um.
Um, you could say, um, shopping center.
And so you kind of get the point.
You can have a lot of different, um, and then, I don't know, um, add a folder shop free and then
as your, you know, you get the point.
You, you can have a lot of folders and models included in your game, which can take it from just game
workspace part to game workspace, dot map to dot shopping center, dot shop pre dot pop.
So you can see how writing that out every single time can get very repetitive.
Okay.
Um which is why we need variables.
Okay.
So how variables work is the term.
You can store any data type to a certain word.
Okay.
So the word you choose B then becomes a keyword.
So whenever you use that word in your in your code, it means whatever the data type is you've assigned
it to.
So think back to data types.
We've got numbers.
We've got booleans.
We've got color threes vector threes enums objects.
Um so even though variables are typically used for objects for referencing okay.
Don't forget that they can be used for lots of other things as well.
Okay.
Um, so let's have a look how to create a variable.
Okay.
So you will first need to use a keyword to create this variable.
A keyword is local okay.
You'll use this word local.
Um.
To create your own keyword.
Most of the time.
So you'd write local to tell it that you're creating.
You're assigning a keyword with something.
Okay.
And after the local, you actually include a space as one of the rare cases that in scripting you include
a space.
Um, obviously we do it with the equal signs, but actually in the in the bit of code, you don't often
include spaces.
Um, we've got local there.
And then when you say the variable name, so the variable name, whatever you want to include, whatever
you want to write it as.
Okay.
You then make that equal to um, any data type.
Okay.
And let's a note this out.
So it's um, a bit better.
So you have the variable name and you have the data type.
Okay.
Um so let's have a look at an example of this.
So let's create a variable which stores the object value of the part.
In short it basically stores the bit that we reference it with.
So I'm going to call this local part because it kind of stores the part is equal to and just reference
it.
I can just say game dot workspace dot map to dot shopping centre dot shop pre dot part.
Okay I have now referenced um that part here.
So the word part is equal to all of this bit of code here.
So later on in my script if I then use the word part I see it as part.
The script sees it as this because it's equal to this word here is equal to this.
So writing this is the exact same, um, it's the exact same as just writing this.
It's just shorter to look at.
Okay.
Which makes it so much easier to do stuff because instead of writing, all of that dot position is equal
to this, you can then just say part dot position is equal to Vector3 dot new 000 for example.
Okay, so it makes it so much easier to write your code and quicker as well.
It makes it easier to understand and read.
So very very useful.
Okay.
Um, but as I explained as well, um, variables are not just for referencing objects, even though
that's what they are commonly used for.
They can also be used for any other data type.
Okay.
So for example you can say local position.
That's a great one.
Let's say local part position.
The reason I don't want to use position is you might get confused with the position property name.
Um, and then we can set that to be a Vector3 value 123 for example.
And I can then say part dot position and get rid of this bit here and just say is equal to part position.
Okay.
Um so again we see it as this.
But the script knows that this here is equal to this data type here.
So it's basically equivalent of just writing that out there.
Okay.
Um so you can hopefully see how it really does shorten down your code and makes it flow much better.
Okay.
Um, a couple now you know how to do them.
There's a couple of things you need to be aware about them.
Um, firstly, they have to be created before you use them.
Okay.
And what I mean by that is you need to write the local bit and assign it to a value.
So assign it to this is the value assigned.
Part two.
For example, before I go ahead and use it in my script, I can't do it the other way around and write
it here because the script hasn't read this bit yet, so it doesn't know what part means.
You need to tell it what it means before you can use it.
Okay.
It's like I needed to tell you what a variable was before you could use a variable.
I can't tell you to use a variable and then later on teach you how to do it.
Teaching comes first okay for telling you how to do it comes first.
And that's exact same with variables here.
Okay.
Second thing is there are some rules when using the name when creating the name of a variable.
Okay.
Um, so um, the name cannot contain any spaces or special characters.
So you can't say part space position has to be or one word and you can't have any special characters.
So don't be putting in um, I don't know.
Um.
Hashtags or weird signs, okay?
You can't.
You can't use those.
Only you can use numbers.
They're fine.
But anything else you don't really want to use as a verb, it also makes it more difficult to use later
on as well.
So not typically the best idea.
Um, they also cannot be a current keyword.
So you cannot create a variable called local print.
Okay, okay.
Because you're you're creating you're creating a new keyword but prints already taken.
It's a keyword which is to do with the print function okay.
So it's already taken.
So you need to use another one.
Okay.
Um, and this isn't a rule, but it's just a very good idea is to make the name of the variable similar
or it's to do with what it stores.
Um, so for example, this here stores the referencing of the part.
So I called it part okay.
I didn't call it something like um, I don't know.
Um.
I didn't call it melon or apple, okay, because it doesn't make sense.
And when you when you use it later on in your script, you see Apple written down like, what did I
what does Apple mean then you've got to go all the way up to to see what it means and then go back down.
It just it makes it so much more difficult.
Okay.
So make make it easier for yourself.
And when you're writing these, make it make it equal to what actually it stores.
So here for example I said part position because I'm assigning it to the parts position value for example.
Okay.
So just make it somewhat similar to what it stores.
Okay.
Um, third is when you might want to use variables.
Okay.
There's one very obvious reason.
And there's one reason which is maybe not as obvious, but you should still know about.
So the most obvious reason is to save yourself time.
Okay.
Um, creating a variable for the part here, um, avoids me writing all of this bit here.
I only have to write that part bit at the end.
And if I'm changing five properties of the part, that's extremely useful.
That takes up a lot of my time.
So it makes things a lot quicker, a lot quicker to read and and obviously write as well.
So that's the first obvious reason.
The second reason is when you have a key value that you might want to change frequently.
Now this one is less common, but it's still you should still know about it.
So let's, for example, say you want to code a sword, for example.
Okay.
And this sword you want to have a it has a couple of values.
Obviously you have the amount of damage the sword deals and the time between each hit that you can make.
Okay.
It would be a good idea to set a variable called damage and make it equal to how much damage you want
the sword to deal, because if then later on you want to reuse your script, but you want to change
the value of damage, or there's maybe there's a game pass you could buy which makes swords deal more
damage.
It makes it much easier to change than if you had written it in your in your code somewhere and your
200 lines of code.
Try and find it.
Okay, so that's the second reason why variables are such a good idea to use.
Okay, so hopefully that gives you a really good example about how variables are so useful.
Okay, similar kind of thing with everything else.
As you see this is just the basics.
So we'll be using this pretty much every single lesson.
So you'll have plenty of examples about how variables are used.
But hopefully this gives you a good overview and a chance for you to try some out for yourself.
Okay, so if you still have some of the scripts that you've used for practicing, um, changing properties,
I'd recommend going back back and that and making a variable for the part and then writing it all out
again, or just getting rid of the bits that you don't need, because you'll find out that how much
easier it is to use basically.
Um, so yeah, one more thing as well.
I should mention, um, variables always, always go at the start.
Okay.
Don't you don't only create a variable when you need, when you just above when you need it.
Okay.
Block them all out at the start.
That's just because it's, um, they're all in one place, and it means that you can use them throughout
your code instead of just one at one bit of your code.
Okay, so quite a bit to learn about variables, but you will become more comfortable with them as you
go along.
Okay.
So have a practice of those and then join me in the next lesson.